package cn.fl.insurance.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.condition.InsPlcPrdDtlTkQueryCondition;
import cn.fl.insurance.dto.InsPlcPrdDtlTkDTO;
import cn.fl.insurance.service.IInsPlcPrdDtlTkApiService;
import cn.fl.insurance.service.IInsPlcPrdDtlTkService;
import cn.fl.insurance.transform.InsPlcPrdDtlTkVOTransform;
import cn.fl.insurance.vo.InsPlcPrdDtlTkVO;
import cn.fl.project.condition.PrjPrdDtlQueryCondition;
import cn.fl.project.dto.PrjPrdDtlDTO;
import cn.fl.project.service.IProjectApiService;
/**
 * @Description: 车类产品保单明细 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsPlcPrdDtlTkServiceImpl implements IInsPlcPrdDtlTkService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(InsPlcPrdDtlTkServiceImpl.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IInsPlcPrdDtlTkApiService insPlcPrdDtlTkApiService;
    @Autowired
    private IProjectApiService projectApiService;

	@Override
	public InsPlcPrdDtlTkVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		InsPlcPrdDtlTkDTO dto = this.insPlcPrdDtlTkApiService.selectInsPlcPrdDtlTkById(id);
		return InsPlcPrdDtlTkVOTransform.toVO(dto);
	}

	@Override
	public Long add(InsPlcPrdDtlTkVO entity) throws BaseException{
		if(null == entity) return null;
		return this.insPlcPrdDtlTkApiService.addInsPlcPrdDtlTk(InsPlcPrdDtlTkVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.insPlcPrdDtlTkApiService.removeInsPlcPrdDtlTkByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(InsPlcPrdDtlTkVO entity) throws BaseException{
		if(null == entity) return false;
		return this.insPlcPrdDtlTkApiService.modifyInsPlcPrdDtlTk(InsPlcPrdDtlTkVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page) page = new Pager();
		page=this.insPlcPrdDtlTkApiService.searchInsPlcPrdDtlTkListPage(page,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		if(CollectionUtils.isNotEmpty(page.getResultList())){
			page.setResultList(InsPlcPrdDtlTkVOTransform.toVOList((List<InsPlcPrdDtlTkDTO>)page.getResultList()));
		}
		return page;
	}
	
	@Override
	public List<InsPlcPrdDtlTkVO> searchList(InsPlcPrdDtlTkQueryCondition condition) throws BaseException{
		List<InsPlcPrdDtlTkVO> list=InsPlcPrdDtlTkVOTransform.toVOList(this.insPlcPrdDtlTkApiService.searchInsPlcPrdDtlTkList(condition));
		if(CollectionUtils.isNotEmpty(list)){
			List<Long> idList=new ArrayList<>(list.size());
			for(InsPlcPrdDtlTkVO vo:list){
				idList.add(vo.getPrjPrdDtlId());
			}
			PrjPrdDtlQueryCondition dtlQueryCondition=new PrjPrdDtlQueryCondition();
			dtlQueryCondition.setIdList(idList);
            List<PrjPrdDtlDTO> dtlDTOList=projectApiService.selectPrjPrdDtlList(dtlQueryCondition);
            if(CollectionUtils.isNotEmpty(dtlDTOList)){
                Map<Long,Long> dtlMap=new HashMap<>(dtlDTOList.size());
                for(PrjPrdDtlDTO dto:dtlDTOList){
                    dtlMap.put(dto.getId(),dto.getActPrdId());
                }
                for(InsPlcPrdDtlTkVO vo:list){
                    if(dtlMap.containsKey(vo.getPrjPrdDtlId())){
                        vo.setActPrdId(dtlMap.get(vo.getPrjPrdDtlId()));
                    }
                }
            }
		}
		return list;
	}
	    
}
